// pages/update/update.js
import axios from "../../http/axios.js";
import {
  apiUpdatePassword,
  apiGetWorerInformation
} from "../../http/server.js";
import {
  md5
} from "../../utils/md5.js";
Page({
  data: {
    titleOne: {
      key: "修改登录密码",
      value: "密码至少6位、数字或者字符组合"
    },
    password: [{
        id: 0,
        key: "原始密码",
        value: "",
        placeholder: "填写原密码"
      },
      {
        id: 1,
        key: "新密码",
        value: "",
        placeholder: "填写新密码"
      },
      {
        id: 2,
        key: "确认秘密",
        value: "",
        placeholder: "再次填写确认"
      }
    ]
  },
  onLoad: function(options) {},
  onReady: function() {},
  onShow: function() {},
  //先进行两次新密码校验是否一致
  checkTwoPassword: function() {
    let password = this.data.password;
    if (!password[0].value.length) {
      wx.showToast({
        title: "请输入原始密码",
        icon: "none"
      })
      return false;
    }
    if (!password[1].value.length) {
      wx.showToast({
        title: "请输入新密码",
        icon: "none"
      })
      return false;
    }
    if (!password[2].value.length) {
      wx.showToast({
        title: "请输入确认密码",
        icon: "none"
      })
      return false;
    }
    if (!(password[1].value.length >= 6)) {
      wx.showToast({
        title: "密码输入长度有误",
        icon: "none"
      })
      return false;
    }
    if (password[1].value != password[2].value) {
      wx.showToast({
        title: "两次输入的密码不一致,请重新输入",
        icon: "none",
      })
      password[1].value = "";
      password[2].value = "";
      this.setData({
        password
      })
      return false;
    };
    if (password[0].value == password[1].value) {
      wx.showToast({
        title: "新密码不能与之前的一致,请重新输入",
        icon: "none"
      })
      password[1].value = "";
      password[2].value = "";
      this.setData({
        password
      })
      return false;
    }
    return true;
  },
  //点击完成
  handleClickSuccess: function() {
    if (!this.checkTwoPassword()) {
      return;
    }
    let userInfo = wx.getStorageSync("userInfo");
    let password = this.data.password;
    let data = {
      jobNumber: userInfo.jobNumber,
      originPassword: md5(password[0].value),
      password: md5(password[2].value)
    }
    axios({
      url: apiUpdatePassword,
      method: "POST",
      data: data
    }).then(res => {
      //当时undefined的时候表示修改成功
      axios({
        url: apiGetWorerInformation,
        data: {
          workersId: userInfo.workersId
        }
      }).then(res => {
        wx.setStorageSync("userInfo", res);
        wx.showToast({
          title: "密码修改成功",
          icon: "none"
        });
        setTimeout(() => {
          wx.navigateBack()
        }, 1500)
      })
    })
  },
  //输入框输入过程中修改
  updateInputValue: function(e) {
    let id = e.detail.id
    let password = this.data.password;
    password.forEach((item, index) => {
      if (item.id == id) {
        item.value = e.detail.value;
      }
    })
    this.setData({
      password
    })
  }
})